package com.su.test;

import com.su.mapper.BrandMapper;
import com.su.pojo.Brand;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Mybatiestest {
    @Test
    public void testSelectAll() throws IOException {
        //1. 加载mybatis的核心配置文件，获取 SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 获取SqlSession对象，用它来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3. 获取代理对象
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        //4. 执行
        List<Brand> brands = brandMapper.selectAll();
        System.out.println(brands);
        //5.关闭资源
        sqlSession.close();
    }

    @Test
    public void selectById() throws IOException {
        int  id = 1;
        //1. 加载mybatis的核心配置文件，获取 SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 获取SqlSession对象，用它来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3. 获取代理对象
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        //4. 执行
        Brand brand = brandMapper.selectById(id);
        System.out.println(brand);

        //5.关闭资源
        sqlSession.close();
    }

    @Test
    public void selectByCondition() throws IOException {
        int status = 1;
        String companyName = "华为";
        String brandName = "华为";
        //因为是模糊查询，要处理一下参数
        companyName = "%" + companyName + "%";
        brandName = "%" + brandName + "%";
    //参数封装对象
        Brand brand = new Brand();
        brand.setStatus(status);
        brand.setCompanyName(companyName);
        brand.setBrandName(brandName);
//Map 封装
        Map map = new HashMap();
//        map.put("status", status);
        map.put("companyName", companyName);
        map.put("brandName", brandName);

        //1. 加载mybatis的核心配置文件，获取 SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 获取SqlSession对象，用它来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3. 获取代理对象
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        //4. 执行
//        List<Brand> brands = brandMapper.selectByCondition(status, companyName, brandName);
//        参数封装对象
//        List<Brand> brands = brandMapper.selectByCondition(brand);
//        System.out.println(brands);

        List<Brand> brands = brandMapper.selectByCondition(map);
        System.out.println(brands);
        //5.关闭资源
        sqlSession.close();
    }
}
